// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "behavior": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "违法行为",
    "label": "违法行为"
  },
  "violation": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "违反条款",
    "label": "违反条款"
  },
  "penalty": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "处罚条款",
    "label": "处罚条款"
  },
  "content": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "处罚内容",
    "label": "处罚内容"
  },
  "doc": {
    "rules": [
      {
        "format": "array"
      },
      {
        "arrayType": "file"
      }
    ],
    "title": "执法文书",
    "label": "执法文书"
  },
  "category": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "经营主体",
            "value": 110
          },
          {
            "text": "食品安全",
            "value": 120
          },
          {
            "text": "特殊食品",
            "value": 125
          },
          {
            "text": "反垄断、竞争",
            "value": 130
          },
          {
            "text": "价格监管",
            "value": 140
          },
          {
            "text": "药品监管",
            "value": 150
          },
          {
            "text": "医疗器械",
            "value": 160
          },
          {
            "text": "化妆品监管",
            "value": 170
          },
          {
            "text": "市场合同",
            "value": 180
          },
          {
            "text": "广告监管",
            "value": 190
          },
          {
            "text": "知识产权",
            "value": 200
          },
          {
            "text": "产品质量",
            "value": 210
          },
          {
            "text": "标准管理",
            "value": 220
          },
          {
            "text": "认证认可",
            "value": 230
          },
          {
            "text": "计量监管",
            "value": 240
          },
          {
            "text": "其他",
            "value": 999
          }
        ]
      }
    ],
    "title": "行为类别",
    "defaultValue": 0,
    "label": "行为类别"
  }
}

const enumConverter = {
  "category_valuetotext": {
    "110": "经营主体",
    "120": "食品安全",
    "125": "特殊食品",
    "130": "反垄断、竞争",
    "140": "价格监管",
    "150": "药品监管",
    "160": "医疗器械",
    "170": "化妆品监管",
    "180": "市场合同",
    "190": "广告监管",
    "200": "知识产权",
    "210": "产品质量",
    "220": "标准管理",
    "230": "认证认可",
    "240": "计量监管",
    "999": "其他"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
